package com.tianqu.tms.www.rental.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.dto.RentalInfoDto;
import com.tianqu.tms.entity.RentalContractInfo;
import com.tianqu.tms.entity.RentalLocationInfo;
import com.tianqu.tms.www.rental.actionform.RentalContractAddInputForm;
import com.tianqu.tms.www.rental.controller.RentalContractAddResultController;
import com.tianqu.tms.www.rental.viewhelper.RentalContractAddInputViewHelper;

/**
 * 合同添加-添加确认 action
 * @author mufeiyu
 *
 */
public class RentalContractAddResultAction extends TmsAction {
	private Logger logger = Logger.getLogger(RentalContractAddResultAction.class);
	private static final String OFF="off";
	@Override
	protected ActionForward doAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext uc) throws Exception {
		
		RentalContractAddInputForm rentalForm = (RentalContractAddInputForm) uc.getInputForm();
		RentalContractAddInputViewHelper rentalView = (RentalContractAddInputViewHelper) uc
		.getAttribute(TmsConstants.VIEWHELP);
		uc.setAttribute(TmsConstants.VIEWHELP, rentalView);
		
		//同一个租位的租期是否重叠check
		SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd");
		//合同开始的日期
		Date startDate = formatDate.parse(rentalForm.getStartDate());
		//合同结束的日期	
		Date endDate = formatDate.parse(rentalForm.getEndDate());

		if ((!StringUtil.isNullOrBlank(rentalForm.getStartDate()))&&(!StringUtil.isNullOrBlank(rentalForm.getEndDate()))) {	
			// 比较
			if (endDate.compareTo(startDate) < 0) {
				String[] param = {"合同结束日期","合同开始日期"};
				setErrorsToRequest(request, TmsConstants.U0036, param); //合同结束日期比合同开始日期小时
				return mapping.getInputForward();
			}
		}
		if(!StringUtil.isNullOrBlank(rentalForm.getSerial())){
			RentalContractInfo contractInfo = RentalContractInfo.getInstance();
			if(contractInfo.isExistRentalLocationBySerial(rentalForm.getSerial())){							
				setErrorsToRequest(request, TmsConstants.U9003, null); //合同编号存在时				
				return mapping.getInputForward();
			}
		}
		if(!StringUtil.isNullOrBlank(rentalForm.getLocationSerial())){
			
			//租位编号是否存在check
			RentalLocationInfo locationInfo = RentalLocationInfo.getInstance();
			if(!locationInfo.isExistLocationInfoBySerial(rentalForm.getLocationSerial())){
				setErrorsToRequest(request, TmsConstants.U9007, null); //租位编号不存在，无法进行保存。				
				return mapping.getInputForward();
			}
			
			
			
			RentalContractInfo contractInfo = RentalContractInfo.getInstance();
			List<RentalInfoDto> rentalInfoDtoList = contractInfo.checkLocationStartDate(rentalForm.getLocationSerial());
//			boolean checkFlg = false;
			if (rentalInfoDtoList != null && rentalInfoDtoList.size() > 0) {
				for (int i=0;i < rentalInfoDtoList.size();i++) {
					RentalInfoDto rentalInfoDto = new RentalInfoDto();
					rentalInfoDto = rentalInfoDtoList.get(i);
					Date oldStartDate = rentalInfoDto.getStartDate();
					Date oldEndDate = rentalInfoDto.getEndDate();
//					if(startDate.compareTo(oldStartDate) >= 0 && startDate.compareTo(oldEndDate) <= 0){
//						checkFlg = true;				
//					}
					if(startDate.compareTo(oldEndDate) <= 0 && endDate.compareTo(oldStartDate) >= 0){
//						checkFlg = true;
						setErrorsToRequest(request, TmsConstants.U9009, null); //合同结束日期比合同开始日期小时
						return mapping.getInputForward();
					}
				}
			}

//			if (checkFlg) {
//				setErrorsToRequest(request, TmsConstants.U9009, null); //合同结束日期比合同开始日期小时
//				return mapping.getInputForward();
//			}
			
		}
		
		RentalContractAddResultController controller = new RentalContractAddResultController();
		String sysPath = request.getSession().getServletContext().getRealPath(
		"/").replace("/tmsweb", "");
		uc.setAttribute("sysPath", sysPath);
		controller.execute(uc);
		
		request.setAttribute(OFF, "信息添加成功!");
		uc.removeAttribute(TmsConstants.VIEWHELP);
		logger.debug(this.getClass().getName() + "#execute was completed.");
		return mapping.findForward(TmsConstants.SUCCESS);
	}
}
